package com.info33.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.exception.TransactionalException;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import com.info33.platform.system.dao.SysTenantUserDao;
import com.info33.platform.system.dto.SysUserDTO;
import com.info33.platform.system.entity.SysTenantUser;
import com.info33.platform.system.service.SysTenantUserService;
import com.info33.platform.system.service.SysUserProfileService;
import com.info33.platform.system.service.SysUserService;
import com.info33.platform.system.util.enums.UserTypeEnum;
import com.info33.platform.system.vo.SysTenantUserVO;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author admin
 */
@Service
public class SysTenantUserServiceImpl extends ServiceImpl<SysTenantUserDao, SysTenantUser> implements SysTenantUserService {

    @Autowired
    private SysTenantUserDao sysTenantUserDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserProfileService sysUserProfileService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysTenantUser register(SysUserDTO dto, Long tid) {
        Long uid = sysUserService
                .register(dto)
                .getId();
        // 校验用户拓展信息
        sysUserProfileService.register(dto.getUserProfileDTO(), tid, uid);
        // 租户与用户绑定
        SysTenantUser tenantUser = sysTenantUserDao.selectOne(new LambdaQueryWrapper<SysTenantUser>()
                .eq(SysTenantUser::getUserId, uid)
                .eq(SysTenantUser::getTenantId, tid)
                .last(TableConstant.LIMIT_ONE));
        Boolean lockedDto = Optional.ofNullable(dto.getTenantUserDTO())
                .map(SysTenantUser::getLocked)
                .orElse(null);
        if (!ObjectUtils.isEmpty(tenantUser)) {
            // 已经注册过
            if (!ObjectUtils.isEmpty(lockedDto)
                    && !tenantUser.getLocked().equals(lockedDto)) {
                // 用户启用与禁用状态值被变更
                SysTenantUser updateLocked = new SysTenantUser();
                updateLocked.setId(tenantUser.getId());
                updateLocked.setLocked(lockedDto);
                if (sysTenantUserDao.updateById(updateLocked) != 1) {
                    throw new TransactionalException();
                }
            }
            return tenantUser;
        }
        // 首次绑定
        tenantUser = new SysTenantUser();
        tenantUser.setUserId(uid);
        tenantUser.setUserType(UserTypeEnum.USER);
        tenantUser.setTenantId(tid);
        tenantUser.setLocked(
                Optional.ofNullable(dto.getTenantUserDTO())
                        .map(SysTenantUser::getLocked)
                        .orElse(Boolean.FALSE));
        if (sysTenantUserDao.installOneWithTenantId(tenantUser) != 1) {
            throw new TransactionalException();
        }
        return tenantUser;
    }

    @Override
    public Result<List<SysTenantUserVO>> pcLi(SysUserVO currentUser) {
        return Result.ok(sysTenantUserDao.pcLi(currentUser, SysUtil.getNoAdminId()));
    }

}